package com.igeek.blog_parent_test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.blog_parent_test.constant.MessageConstant;
import com.igeek.blog_parent_test.entity.Albums;
import com.igeek.blog_parent_test.entity.Users;
import com.igeek.blog_parent_test.result.Result;
import com.igeek.blog_parent_test.service.IAlbumsService;
import com.igeek.blog_parent_test.service.impl.AlbumsServiceImpl;
import com.igeek.blog_parent_test.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-04
 */
@RestController
@RequestMapping("admin/albums")
@Api(tags = "相册接口管理")
public class AlbumsController {
    @Autowired
    private IAlbumsService IAlbumsService;
    @Autowired
    private AlbumsServiceImpl albumsServiceImpl;

    @ApiOperation("分页条件查询相册")
    @GetMapping("findPage")
    public Result findpage(@ApiParam("分页参数") PageQueryVO vo){
        Page<Albums>page= IAlbumsService.findPage(vo);
        if(page!=null){
            return new Result(true, MessageConstant.QUERY_ALBUMS_SUCCESS,page);
        }
        return new Result(false,MessageConstant.QUERY_ALBUMS_FAIL);
    }

    @ApiOperation("新增相册")
    @PostMapping("add")
    public Result add(@RequestBody Albums albums) {
        boolean flag = IAlbumsService.save(albums);
        if (flag) {
            return new Result(true, MessageConstant.ADD_ALBUMSINFO_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_ALBUMSINFO_FAIL);
    }

    @ApiOperation("通过ID查询相册信息")
    @GetMapping("{id}")
    public Result findOne(@PathVariable("id") Integer id) {
        Albums albums = IAlbumsService.getById(id);
        if(ObjectUtils.isEmpty(albums)){
            return new Result(true, MessageConstant.QUERY_ALBUMS_SUCCESS,albums);
        }
        return new Result(false, MessageConstant.QUERY_ALBUMS_FAIL);
    }


    @ApiOperation("编辑相册信息")
    @PutMapping("update")
    public Result update(@RequestBody Albums albums) {
        boolean flag = IAlbumsService.updateById(albums);
        if (flag) {
            return new Result(true, MessageConstant.EDIT_ALBUMSINFO_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_ALBUMSINFO_FAIL);
    }

    @ApiOperation("删除相册")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean flag = IAlbumsService.del(id);
        if (flag) {
            return new Result(true, MessageConstant.DELETE_ALBUMSINFO_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_ALBUMSINFO_FAIL);
    }
}


